package top.lingkang.fas.admin.config;


import cn.hutool.core.util.StrUtil;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import top.lingkang.fas.admin.utils.RedissonUtil;

/**
 * @author lingkang
 * created by 2025/7/19 23:55
 */
@Configuration
public class RedisConfig {

    @Inject("${redis.address}")
    private String address;
    @Inject("${redis.database:0}")
    private Integer database;
    @Inject("${redis.password}")
    private String password;

    @Inject("${redis.pool.min-idle:1}")
    private Integer minIdle;
    @Inject("${redis.pool.max-idle:8}")
    private Integer maxIdle;
    @Inject("${redis.pool.max-active:8}")
    private Integer timeout;

    @Bean
    public RedissonClient redissonClient() {
        // 创建配置
        Config config = new Config();

        // 单机模式 - 连接本地Redis
        config.useSingleServer()
                .setAddress(address)
                .setDatabase(database)
                .setPassword(StrUtil.isBlank(password) ? null : password) // 如果有密码，设置为实际密码
                .setTimeout(timeout)
                .setConnectionPoolSize(maxIdle) // 连接池大小
                .setConnectionMinimumIdleSize(minIdle); // 最小空闲连接数


        // 明确设定 Jackson 序列化器（默认配置）
        config.setCodec(new JsonJacksonCodec());

        // 集群模式示例
        /*
        config.useClusterServers()
              .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001")
              .addNodeAddress("redis://127.0.0.1:7002");
        */

        // 创建Redisson客户端
        RedissonClient redissonClient = Redisson.create(config);
        RedissonUtil.initialize(redissonClient);
        return redissonClient;
    }
}
